LIBRARY ieee;
USE ieee.std_logic_1164.ALL;

USE ieee.std_logic_arith.ALL;
use STD.textio.all;


ENTITY read_file IS
END read_file;

ARCHITECTURE beha OF read_file IS

    signal  bin_value : bit_vector(31 downto 0):="00000000000000000000000000000000";
   
BEGIN
   
   --Read process
    process
      file file_pointer : text;
        variable line_content : string(1 to 32);
      variable line_num : line;
        variable j : integer := 0;
        variable char : character :='0';
   begin
        --Open the file read.txt from the specified location for reading(READ_MODE).
      file_open(file_pointer,"C:\Users\Tristan Laptop\workspaceSigasi\EngiMultiplier\1.txt",READ_MODE);   
      while not endfile(file_pointer) loop --till the end of file is reached continue.
      readline (file_pointer,line_num);  --Read the whole line from the file
        --Read the contents of the line from  the file into a variable.
      READ (line_num,line_content);
        --For each character in the line convert it to binary value.
        --And then store it in a signal named 'bin_value'.
        for j in 1 to 32 loop       
            char := line_content(j);
            if(char = '0') then
                bin_value(32-j) <= '0';
            else
                bin_value(32-j) <= '1';
            end if;
        end loop;  
        wait for 10 ns; --after reading each line wait for 10ns.
      end loop;
      file_close(file_pointer);  --after reading all the lines close the file. 
        wait;
    end process;
    
    --Write process
    process
      file file_pointerOut : text;
        variable line_contentOut : string(1 to 64);
        variable bin_value : std_logic_vector(63 downto 0);
      variable line_numOut : line;
        variable i,j : integer := 0;
        variable charOut : character:='0';
   begin
        --Open the file write.txt from the specified location for writing(WRITE_MODE).
      file_open(file_pointerOut,"C:\Users\Tristan Laptop\workspaceSigasi\EngiMultiplier\2.txt",WRITE_MODE);     
        --We want to store binary values from 0000 to 1111 in the file.
      for i in 0 to 30 loop --length of file being created
        bin_value := conv_std_logic_vector(i,64);
        --convert each bit value to character for writing to file.
        for j in 0 to 63 loop
            if(bin_value(j) = '0') then
                line_contentOut(64-j) := '0';
            else
                line_contentOut(64-j) := '1';
            end if;
        end loop;
        write(line_numOut,line_contentOut); --write the line.
      writeline (file_pointerOut,line_numOut); --write the contents into the file.
        wait for 10 ns;  --wait for 10ns after writing the current line.
      end loop;
      file_close(file_pointerOut); --Close the file after writing.
        wait;
    end process;

end beha;